#include <QApplication>
#include <QHBoxLayout>
#include <QLabel>
#include <QSlider>
#include <QString>
#include <QVBoxLayout>
#include <QWidget>
#include <iostream>
#include <qDebug>
#include <thread>

int g1, g2, g3;
bool trigger = false;

void change_value1(int value) {}

void img_proc() { qDebug() << "Slider 1 value: "; }

void img_loop() {
  while (true) {
    if (trigger) {
      img_proc();

      trigger = false;
    }
  }
}

int main(int argc, char* argv[]) {
  QApplication app(argc, argv);

  // 创建主窗口
  QWidget mainWindow;
  mainWindow.setWindowTitle("Image and Sliders");
  mainWindow.resize(400, 300);

  // 创建布局
  QVBoxLayout* mainLayout = new QVBoxLayout(&mainWindow);

  // 添加图片
  QLabel* imageLabel = new QLabel(&mainWindow);
  QPixmap pixmap("shadow.jpg");  // 替换为实际图片路径
  imageLabel->setPixmap(pixmap);
  mainLayout->addWidget(imageLabel);

  // 添加滑动条和显示数值的标签
  QSlider* slider1 = new QSlider(Qt::Horizontal, &mainWindow);
  QSlider* slider2 = new QSlider(Qt::Horizontal, &mainWindow);
  QSlider* slider3 = new QSlider(Qt::Horizontal, &mainWindow);

  QLabel* valueLabel1 = new QLabel(&mainWindow);
  QLabel* valueLabel2 = new QLabel(&mainWindow);
  QLabel* valueLabel3 = new QLabel(&mainWindow);

  mainLayout->addWidget(slider1);
  mainLayout->addWidget(valueLabel1);
  mainLayout->addWidget(slider2);
  mainLayout->addWidget(valueLabel2);
  mainLayout->addWidget(slider3);
  mainLayout->addWidget(valueLabel3);

  // 将滑动条的值变化信号与更新标签文本的槽函数关联
  QObject::connect(slider1, &QSlider::valueChanged, [&](int value) {
    valueLabel1->setText(QString("Slider 1: %1").arg(value));
    g1 = value;  // qDebug() << "g1" << g1;
    trigger = true;
  });

  QObject::connect(slider2, &QSlider::valueChanged, [&](int value) {
    valueLabel2->setText(QString("Slider 2: %1").arg(value));
    g2 = value;
    trigger = true;
  });

  QObject::connect(slider3, &QSlider::valueChanged, [&](int value) {
    valueLabel3->setText(QString("Slider 3: %1").arg(value));
    g3 = value;
    trigger = true;
  });

  std::thread work(img_loop);
  work.detach();

  mainWindow.setLayout(mainLayout);
  mainWindow.show();

  return app.exec();
}
